<?php
class AJD_User
{
	var $_username;
	var $_is_temp = false;
	var $_firstname;
	var $_lastname;
	var $_id;
	var $_email;
	var $_usertable;
	var $_db;

    function setFirstName($firstname)
    {
        $this->_firstname = trim($firstname);
    }

    function getFirstName()
    {
        return $this->_firstname;
    }

    function setLastName($lastname)
    {
        $this->_lastname = trim($lastname);
    }

    function getLastName()
    {
        return $this->_lastname;
    }

    function setId($id)
    {
        $this->_id = trim($id);
    }

    function getId()
    {
        return $this->_id;
    }

    function setEmail($email)
    {
        $this->_email = trim($email);
    }

    function getEmail()
    {
        return $this->_email;
    }

    function setUserName($username)
    {
        $this->_username = trim($username);
    }

    function getUserName()
    {
        return $this->_username;
    }

    function setPassword($password)
    {
        $this->_password = trim($password);
    }

    function getPassword()
    {
        return $this->_password;
    }

	function setUserTable($usertable)
	{
		$this->_usertable = trim($usertable);
	}

	function getUserTable()
	{
		return $this->_usertable;
	}

	function getAttributes()
	{
		$username = $this->_username;
		$sql = "SELECT firstname,lastname,id,email FROM ".$this->_usertable." WHERE username = '".$username."'";
		$row = $this->_db->singleselect($sql);
		$this->setFirstName($row['firstname']);
		$this->setLastName($row['lastname']);
		$this->setId($row['id']);
		$this->setEmail($row['email']);
		return true;
	}

    function login()
    {
    	$username = $this->_username;
    	$password = $this->_password;
		$sql = "SELECT count(*) mycheck FROM ".$this->_usertable." WHERE username = '".$username."' and password = '".$password."'";
		$row = $this->_db->singleselect($sql);
		if ($row['mycheck'] == '1') {
			$this->getAttributes();
			return true;
		}
		else
		{
			return false;
		}
    }

	function showUser()
	{
		$res = "User ID: $this->_id, First Name: $this->_firstname, Last Name: $this->_lastname";
		return $res;
	}
	
	function showSession()
	{
		return var_dump($_SESSION);
	}
	
    function changePassword()
    {

    }

    function __construct($username = false, $password = false, $application)
    {
        if ($username && $password) {
        	$this->setUserTable($application->getUserTable());
            $this->setUserName($username);
            $this->setPassword($password);
    		$this->_db = $_SESSION['db'];           
        }
        else
        {

        }
    }

    function isTemp($is_temp = null)
    {
        if (null !== $is_temp) {
            $this->_is_temp = (bool) $is_temp;
        }
        return $this->_is_temp;
    }

    function __destruct()
    {
        $this->cleanUp();
    }

    function delete($db)
    {
        return $db->delete($this->getId());
    }

    function cleanUp()
    {
        if ($this->isTemp()) {
            return $this->delete();
        }
        return true;
    }

}
?>